﻿using System;
using System.IO;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Text;
using System.Web;

using Obsidian.Edm;

namespace Obsidian.Web
{
    /// <summary>
    /// 参数接收
    /// author:郑金泉
    /// update:2010-05-04
    /// </summary>
    public class ParamsReceiver
    {

        #region Request

        /// <summary>
        /// 检查参数是否都存在
        /// </summary>
        /// <param name="arrParams">参数</param>
        /// <returns>是否都存在</returns>
        public static bool RequestExists(params string[] args)
        {
            bool exists = true;
            HttpRequest request = HttpContext.Current.Request;
            foreach (string arg in args)
            {
                if (request[arg] == null)
                {
                    exists = false;
                    break;
                }
            }
            return exists;
        }

        public static T GetModel<T>() where T : OModel, new()
        {
            T o = new T();
            IModelField[] fields = o.Fields;
            foreach (IModelField f in fields)
            {
                string alias = f.Alias;
                if (!String.IsNullOrEmpty(alias))
                    f.TrySet(GetRequestString(alias));
            }
            return o;
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名称</param>
        /// <returns>值</returns>
        public static string GetRequestString(string paramName)
        {
            HttpRequest request = HttpContext.Current.Request;
            return request[paramName] == null ? null : request[paramName];
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <param name="result">输出值</param>
        public static bool TryGetString(string paramName, out string result)
        {
            result = GetRequestString(paramName);
            return result != null;
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <returns>值</returns>
        public static int GetInt32(string paramName)
        {
            return int.Parse(GetRequestString(paramName));
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <param name="result">输出值</param>
        public static bool TryGetInt32(string paramName, out int result)
        {
            return int.TryParse(GetRequestString(paramName), out result);
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <returns>值</returns>
        public static long GetInt64(string paramName)
        {
            return long.Parse(GetRequestString(paramName));
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <param name="result">输出值</param>
        public static bool TryGetInt64(string paramName, out long result)
        {
            return long.TryParse(GetRequestString(paramName), out result);
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <returns>值</returns>
        public static decimal GetDecimal(string paramName)
        {
            return decimal.Parse(GetRequestString(paramName));
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <param name="result">输出值</param>
        public static bool TryGetDecimal(string paramName, out decimal result)
        {
            return decimal.TryParse(GetRequestString(paramName), out result);
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <returns>值</returns>
        public static bool GetBoolean(string paramName)
        {
            return bool.Parse(GetRequestString(paramName));
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <param name="result">输出值</param>
        public static bool TryGetBoolean(string paramName, out bool result)
        {
            return bool.TryParse(GetRequestString(paramName), out result);
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <returns>值</returns>
        public static DateTime GetDateTime(string paramName)
        {
            return DateTime.Parse(GetRequestString(paramName));
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <param name="result">输出值</param>
        public static bool TryGetDateTime(string paramName, out DateTime result)
        {
            return DateTime.TryParse(GetRequestString(paramName), out result);
        }

        /// <summary>
        /// 获得输入流数据的字符串
        /// </summary>
        /// <returns></returns>
        public static string GetRequestInputStreamString()
        {
            HttpRequest request = HttpContext.Current.Request;
            string streamString = null;
            Stream stream = request.InputStream;
            Byte[] byteArr = new Byte[stream.Length];
            stream.Read(byteArr, 0, Convert.ToInt32(stream.Length));
            streamString = Encoding.UTF8.GetString(byteArr);
            return streamString;
        }

        #endregion

        #region Form

        /// <summary>
        /// 检查参数是否都存在
        /// </summary>
        /// <param name="arrParams">参数</param>
        /// <returns>是否都存在</returns>
        public static bool FormExists(params string[] args)
        {
            bool exists = true;
            NameValueCollection form = HttpContext.Current.Request.Form;
            foreach (string arg in args)
            {
                if (form[arg] == null)
                {
                    exists = false;
                    break;
                }
            }
            return exists;
        }

        /// <summary>
        /// 获取参数并转为对应实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T GetFormModel<T>() where T : OModel, new()
        {
            T o = new T();
            IModelField[] fields = o.Fields;
            foreach (IModelField f in fields)
                f.TrySet(GetFormString(f.Alias));
            return o;
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名称</param>
        /// <returns></returns>
        public static string GetFormString(string paramName)
        {
            NameValueCollection form = HttpContext.Current.Request.Form;
            return form[paramName] == null ? null : form[paramName];
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <param name="result">输出值</param>
        public static bool TryGetFormString(string paramName,out string result)
        {
            result = GetFormString(paramName);
            return result != null;
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <returns>值</returns>
        public static int GetFormInt32(string paramName)
        {
            string ps = GetFormString(paramName);
            return int.Parse(ps);
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <param name="result">输出值</param>
        public static bool TryGetFormInt32(string paramName, out int result)
        {
            return int.TryParse(GetFormString(paramName), out result);
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <returns>值</returns>
        public static long GetFormInt64(string paramName)
        {
            return long.Parse(GetFormString(paramName));
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <param name="result">输出值</param>
        public static bool TryGetFormInt64(string paramName,out long result)
        {
            return long.TryParse(GetFormString(paramName), out result);
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <returns>值</returns>
        public static decimal GetFormDecimal(string paramName)
        {
            return decimal.Parse(GetFormString(paramName));
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <param name="result">输出值</param>
        public static bool TryGetFormDecimal(string paramName, out decimal result)
        {
            return decimal.TryParse(GetFormString(paramName), out result);
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <returns>值</returns>
        public static bool GetFormBoolean(string paramName)
        {
            return bool.Parse(GetFormString(paramName));
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <param name="result">输出值</param>
        public static bool TryGetFormBoolean(string paramName,out bool result)
        {
            return bool.TryParse(GetFormString(paramName), out result);
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <returns>值</returns>
        public static DateTime GetFormDateTime(string paramName)
        {
            return DateTime.Parse(GetFormString(paramName));
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <param name="result">输出值</param>
        public static bool TryGetFormDateTime(string paramName,out DateTime result)
        {
            return DateTime.TryParse(GetFormString(paramName), out result);
        }

        #endregion

        #region Query

        /// <summary>
        /// 检查参数是否都存在
        /// </summary>
        /// <param name="arrParams">参数</param>
        /// <returns>是否都存在</returns>
        public static bool QueryExists(params string[] args)
        {
            bool exists = true;
            NameValueCollection query = HttpContext.Current.Request.QueryString;
            foreach (string arg in args)
            {
                if (query[arg] == null)
                {
                    exists = false;
                    break;
                }
            }
            return exists;
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名称</param>
        /// <returns>值</returns>
        public static string GetQueryString(string paramName)
        {
            NameValueCollection query = HttpContext.Current.Request.QueryString;
            return query[paramName] == null ? null : query[paramName];
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <param name="result">输出值</param>
        public static bool TryGetQueryString(string paramName,out string result)
        {
            result = GetQueryString(paramName);
            return result != null;
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <returns>值</returns>
        public static int GetQueryInt32(string paramName)
        {
            return int.Parse(GetQueryString(paramName));
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <param name="result">输出值</param>
        public static bool TryGetQueryInt32(string paramName, out int result)
        {
            return int.TryParse(GetQueryString(paramName), out result);
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <returns>值</returns>
        public static long GetQueryInt64(string paramName)
        {
            return long.Parse(GetQueryString(paramName));
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <param name="result">输出值</param>
        public static bool TryGetQueryInt64(string paramName, out long result)
        {
            return long.TryParse(GetQueryString(paramName), out result);
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <returns>值</returns>
        public static decimal GetQueryDecimal(string paramName)
        {
            return decimal.Parse(GetQueryString(paramName));
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <param name="result">输出值</param>
        public static bool TryGetQueryDecimal(string paramName, out decimal result)
        {
            return decimal.TryParse(GetQueryString(paramName), out result);
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <returns>值</returns>
        public static bool GetQueryBoolean(string paramName)
        {
            return bool.Parse(GetQueryString(paramName));
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <param name="result">输出值</param>
        public static bool TryGetQueryBoolean(string paramName,out bool result)
        {
            return bool.TryParse(GetQueryString(paramName), out result);
        }

        /// <summary>
        /// 获得参数值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <returns>值</returns>
        public static DateTime GetQueryDateTime(string paramName)
        {
            return DateTime.Parse(GetQueryString(paramName));
        }

        /// <summary>
        /// 获得参数值,获取失败时取默认值
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <param name="result">输出值</param>
        public static bool TryGetQueryDateTime(string paramName, out DateTime result)
        {
            return DateTime.TryParse(GetQueryString(paramName), out result);
        }

        #endregion
        /*
        public static byte[] GetPostedFileBytes(int index)
        {
            HttpRequest request = HttpContext.Current.Request;
            HttpPostedFile file = request.Files[index];

            int fileLen = file.ContentLength;
            byte[] input = new byte[fileLen];
            Stream stream = file.InputStream;
            stream.Read(input, 0, fileLen);

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < fileLen; i++)
                sb.Append(input[i].ToString());

            return 
        }
         * */

    }
}
